package day1130book;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import day1130.JDBCUtil;

public class BookcardDao {
	
	//添加一个新的借书卡
	public static void insert(Bookcard bc) {
		String sql = "insert into bookcard(id) values(?) ";
		Connection conn = null;
		PreparedStatement ps = null;
		try {
			conn = JDBCUtil.getConnection();
			ps = conn.prepareStatement(sql);
			ps.setInt(1, bc.getId());
			ps.execute();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			JDBCUtil.close(conn, ps);
		}
	}
	
	//查找借书卡卡号
	public static ArrayList<Bookcard> selectId(int id) {
		String sql = "select * from bookcard where id = ? ";
		int res = 0;
		ArrayList<Bookcard> list = new ArrayList<Bookcard>();
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet result = null;
		try {
			conn = JDBCUtil.getConnection();
			ps = conn.prepareStatement(sql);
			ps.setInt(1, id);
			result = ps.executeQuery();
			while(result.next()) {
				Bookcard bc = new Bookcard();
				bc.setId(result.getInt("id"));
				list.add(bc);
			}
			return list;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			JDBCUtil.close(conn, ps);
		}
		
		return null;
	}
    
}
